package com.vst.orm.base.exception;

import java.io.Serializable;

import com.vst.orm.base.Idable;




public class PersistenceException extends RuntimeException {
	private static final long serialVersionUID = 1L;

	public PersistenceException() {
		super();
	}

	public PersistenceException(Class<?> type, Serializable id, String action,
			String msg, Throwable throwable) {
		super(String.format("%s类型%s, id = %s: %s", action, type.getName(),
				String.valueOf(id), msg), throwable);
	}

	public PersistenceException(Object entity, String action, String msg,
			Throwable throwable) {
		this(entity.getClass(), id(entity), action, msg, throwable);
	}

	private static Serializable id(Object entity) {
		if (entity instanceof Idable)
			return ((Idable<?>) entity).getId();
		return "?";
	}
}
